/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package barratt.cef.command.listener;

import barratt.cef.command.FitBarrattModelCommand;
import cef.command.CommandI;
import cef.command.listener.CommandOutputEvent;
import cef.command.listener.CommandOutputListener;
import cef.command.listener.CommandOutputListenerException;
import cef.command.listener.CommandPrintWriter;
import commons.util.PrintUtil;
import org.stat.model.data.Record;

import java.util.Set;
import java.util.SortedMap;

import static commons.util.FormatUtil.format;

/**
 *
 *
 * @version 1.3.0
 * @author Susanta Tewari
 * @history Created on 12/22/12
 * @since 1.3.0
 */
public class FitBarrattModelPrintWriter extends CommandPrintWriter {

    @Override
    public void receivedEventImpl(final CommandOutputEvent cmdOutputEvent)
            throws CommandOutputListenerException {

        final FitBarrattModelCommand command = (FitBarrattModelCommand) cmdOutputEvent.getSource();


        // MSE Comparison
        getPrintWriter().println("MSE Comparison: \n");

        final SortedMap<Double, Double> mseMap = command.getMseMap();
        final Set<Double> keySet               = mseMap.keySet();
        final String[][] result1               = new String[keySet.size() + 1][2];

        result1[0] = new String[] { "MSE", "K" };

        int i1 = 0;

        for (final Double mse : keySet) {

            i1++;

            result1[i1] = new String[] { format(mse), format(mseMap.get(mse)) };
        }

        getPrintWriter().println(PrintUtil.print2D(result1, 2, 1, null));


        // best k
        getPrintWriter().println("\nBest K :" + command.getK());


        // obsreved vs estimated
        getPrintWriter().println("\n");

        final Record[] records  = command.getRecords();
        final Object[][] result = new Object[records.length + 1][3];

        result[0] = new String[] { "Observed", "Estimated", "ObservedX" };

        for (int i = 0; i < records.length; i++) {

            final Record record    = records[i];
            final String observed  = format(record.getData()[0].getValue());
            final String observedX = format(record.getData()[1].getValue());
            final String estimated = format(record.getData()[0].getEstimatedValue());

            result[i + 1] = new String[] { observed, estimated, observedX };
        }

        getPrintWriter().println(PrintUtil.print2D(result, 2, 1, null));
    }

    @Override
    public CommandOutputListener newInstance() {
        return new FitBarrattModelPrintWriter();
    }

    @Override
    public Class<? extends CommandI> getTargetCommandClass() {
        return FitBarrattModelCommand.class;
    }
}
